/**
 * CircleAdapter.java
 * com.jamin.aabill.ui
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-27 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */
package com.jamin.aabill.ui;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jamin.aabill.R;
import com.jamin.aabill.data.Circle;
import com.jamin.aabill.utils.TimeUtils;



/**
 * ClassName:CircleAdapter
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-7-27	下午2:43:40
 */
class CircleAdapter extends BaseAdapter{

	List<Circle> circleList = null;
	LayoutInflater inflater = null;


	public CircleAdapter(List<Circle> circleList, Context ctx){
		inflater = LayoutInflater.from(ctx);
		this.circleList = circleList;
	}

	public void notifyAdapter(List<Circle> circleList){
		this.circleList = circleList;
	}
	
	@Override
	public int getCount() {
		return circleList.size();
	}

	@Override
	public Object getItem(int position) {
		return circleList.get(position);

	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Circle circle = circleList.get(position);
		/**
		 * 做UI缓存，优化ListView
		 */
		ViewHolder viewHolder = null;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = inflater.inflate( R.layout.circle_list_item,  null );
			viewHolder.mCircleLayout = (RelativeLayout) convertView.findViewById(R.id.circle_item_layout); 
			viewHolder.mCircleIconIV = (ImageView) convertView.findViewById(R.id.circle_item_icon);
			viewHolder.mCircleNameTV = (TextView) convertView.findViewById(R.id.circle_item_name);
			viewHolder.mCircleDescriptionTV = (TextView) convertView.findViewById(R.id.circle_item_description);
			viewHolder.mCircleTimeTV = (TextView) convertView.findViewById(R.id.circle_item_time);
			viewHolder.mCircleConTV = (TextView) convertView.findViewById(R.id.circle_item_contribution);
			viewHolder.mCircleIdTV = (TextView) convertView.findViewById(R.id.circle_item_id);
			convertView.setTag(viewHolder);

		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		/**
		 * 根据circle的状态设置背景
		 */
		if(circle.getActive() == 1){
//			if(circle.getCircle_contribution() >= 0){
//				viewHolder.mCircleLayout.setBackgroundResource(R.drawable.positive_bg);
//			}else{
//				viewHolder.mCircleLayout.setBackgroundResource(R.drawable.negative_bg);
//			}
			viewHolder.mCircleIconIV.setBackgroundResource(R.drawable.checkbox_checked);
		}else{
			viewHolder.mCircleLayout.setBackgroundColor(R.color.grey_hint);
			viewHolder.mCircleIconIV.setBackgroundResource(R.drawable.checkbox_unchecked);
		}
		/**
		 * 其他数据
		 */
		viewHolder.mCircleIdTV.setText(String.valueOf(circle.getId()));
		viewHolder.mCircleNameTV.setText(circle.getCircle_name());
		viewHolder.mCircleDescriptionTV.setText(circle.getCircle_description());
		viewHolder.mCircleTimeTV.setText(TimeUtils.FormatTimeForm(circle.getCircle_create_time()));
		viewHolder.mCircleConTV.setText(AABillApplication.mApplicationContext.getString(R.string.circle_bill_cost, 
				AABillApplication.mBillManager.getCircleTotalCost(circle.getId())));
		return convertView;

	}

	class ViewHolder{

		RelativeLayout mCircleLayout = null;
		ImageView mCircleIconIV = null;
		TextView mCircleNameTV = null;
		TextView mCircleDescriptionTV = null;
		TextView mCircleTimeTV = null;
		TextView mCircleConTV = null;
		TextView mCircleIdTV = null;

	}

}
